﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_ReturnDetailLogBusiness : BaseBusiness<PB_ReturnDetailLog>, IPB_ReturnDetailLogBusiness, ITransientDependency
    {
        readonly IOperator _operator;

        public PB_ReturnDetailLogBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;

        }

        #region 外部接口

        public async Task<PageResult<PB_ReturnDetailLog>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_ReturnDetailLog>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_ReturnDetailLog, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_ReturnDetailLog> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(SaveDataPB_ReturnDetailLogDTO data)
        {
            List<PB_ReturnDetailLog> SubListMain = new List<PB_ReturnDetailLog>();
            for (int i = 0; i < data.SubList?.Count; i++)
            {
                await InsertAsync(data.SubList[i]);

            }

        }

        public async Task UpdateDataAsync(SaveDataPB_ReturnDetailLogDTO data)
        {
            //List<PB_ReturnDetailLog> SubListMain = new List<PB_ReturnDetailLog>();
            //String ID = IdHelper.GetId();
            //PB_Return pB_Return = new PB_Return();
            //pB_Return.Id = ID;
            //pB_Return.CreatorId = _operator.UserId;
            //pB_Return.CreateTime = DateTime.Now;
            //pB_Return.CreatorRealName = _operator.UserId;
            //pB_Return.Deleted = false;
            //pB_Return.State = 2;

            //await Db.InsertAsync<PB_Return>(pB_Return);
            //PB_ReturnDetailLog pB_ReturnDetailLog = new PB_ReturnDetailLog();
            //for (int i = 0; i < data.SubList?.Count; i++)
            //{
            //    string SubId = IdHelper.GetId();
            //    pB_ReturnDetailLog.Id = SubId;
            //    pB_ReturnDetailLog.ParentId = ID;
            //    pB_ReturnDetailLog.ProjectId = data.SubList[i].ProjectId;
            //    pB_ReturnDetailLog.MaterialId = data.SubList[i].MaterialId;
            //    pB_ReturnDetailLog.MaterialCode = data.SubList[i].MaterialCode;
            //    pB_ReturnDetailLog.MaterialName = data.SubList[i].MaterialName;
            //    pB_ReturnDetailLog.MaterialSpec = data.SubList[i].MaterialSpec;
            //    pB_ReturnDetailLog.DeliverNums = data.SubList[i].DeliverNums;
            //    pB_ReturnDetailLog.SortingSum = data.SubList[i].SortingSum;
            //    pB_ReturnDetailLog.ReturnNums = data.SubList[i].ReturnNums;
            //    pB_ReturnDetailLog.Remarks = data.SubList[i].Remarks;
            //    pB_ReturnDetailLog.CreatorRealName = _operator?.UserId;
            //    pB_ReturnDetailLog.Deleted = false;
            //    pB_ReturnDetailLog.QrCode = "27," + data.SubList[i].Id;
            //    pB_ReturnDetailLog.CreateTime = DateTime.Now;//创建时间
            //    pB_ReturnDetailLog.CreatorId = _operator?.UserId; //创建人
            //    pB_ReturnDetailLog.ThisReturnNums = data.SubList[i].ThisReturnNums;
            //    pB_ReturnDetailLog.ThisReturnNums = data.SubList[i].ThisReturnNums;
            //    SubListMain.Add(pB_ReturnDetailLog);
            //    await InsertAsync(SubListMain);

            List<PB_ReturnDetailLog> SubListMain = new List<PB_ReturnDetailLog>();
            PB_ReturnDetailLog pB_ReturnDetailLog = new PB_ReturnDetailLog();
            for (int i = 0; i < data.SubList?.Count; i++)
            {
                pB_ReturnDetailLog.Id = data.SubList[i].Id;
                //pB_ReturnDetailLog.ParentId = data.SubList[i].ParentId;
                //pB_ReturnDetailLog.ProjectId = data.SubList[i].ProjectId;
                //pB_ReturnDetailLog.MaterialId = data.SubList[i].MaterialId;
                //pB_ReturnDetailLog.MaterialCode = data.SubList[i].MaterialCode;
                //pB_ReturnDetailLog.MaterialName = data.SubList[i].MaterialName;
                //pB_ReturnDetailLog.MaterialSpec = data.SubList[i].MaterialSpec;
                //pB_ReturnDetailLog.DeliverNums = data.SubList[i].DeliverNums;
                pB_ReturnDetailLog.SortingSum = data.SubList[i].SortingSum;
                //pB_ReturnDetailLog.ReturnNums = data.SubList[i].ReturnNums;
                //pB_ReturnDetailLog.Remarks = data.SubList[i].Remarks;
                //pB_ReturnDetailLog.CreatorRealName = _operator?.UserName;
                //pB_ReturnDetailLog.Deleted = false;
                //pB_ReturnDetailLog.QrCode = "27," + data.SubList[i].Id;
                //pB_ReturnDetailLog.CreateTime = DateTime.Now;//创建时间
                //pB_ReturnDetailLog.CreatorId = _operator?.UserId; //创建人
                //SubListMain.Add(pB_ReturnDetailLog);
                List<string> props = GetFieldList(pB_ReturnDetailLog);
                await UpdateAsync(pB_ReturnDetailLog, props);

            }



        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}